package com.liu.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.shop.common.utils.PageUtils;
import com.liu.shop.common.utils.R;
import com.liu.shop.common.vo.UserVo;
import com.liu.user.entity.ThirdUserEntity;
import com.liu.user.entity.UserEntity;
import com.liu.user.service.ThirdUserService;
import com.liu.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 
 *
 * @author lls
 * @email 302140431@qq.com
 * @date 2021-07-30 17:30:21
 */
@RestController
@RequestMapping("/user/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private ThirdUserService thirdUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 查询单个，传入字段和值
     */
    @PostMapping("/findByName")
    public UserEntity findByName(String field, String value){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(field,value);
        UserEntity one = userService.getOne(queryWrapper);
        return one;
    }

    @GetMapping("/queryUser")
    public UserVo queryUser(@RequestParam("name") String name) {
        return userService.queryUser(name);
    }

    /**
     * 查询第三方数据
     * @param field
     * @param value
     * @return
     */
    @PostMapping("/findThirdUser")
    public ThirdUserEntity findThirdUser(String field, String value){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(field,value);
        ThirdUserEntity one = thirdUserService.getOne(queryWrapper);
        return one;
    }

    /**
     * 用户第一次用第三方软件登录，注册到第三方用户表中
     * @param thirdUser
     * @return
     */
    @RequestMapping("/saveThirdUser")
    public R saveThirdUser(@RequestBody ThirdUserEntity thirdUser){
        thirdUserService.save(thirdUser);
        return R.ok();
    }


}
